package com.yiqi.liebang.feature.mine.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.DateTool;
import com.yiqi.liebang.common.widget.dialog.ActionSheetDialog;
import com.yiqi.liebang.common.widget.dialog.OnOperItemClickL;
import com.yiqi.liebang.entity.bo.EducationBo;
import com.yiqi.liebang.entity.bo.ImgBean;
import com.yiqi.liebang.entity.bo.UserCardBo;
import com.yiqi.liebang.entity.bo.WorkBo;
import com.yiqi.liebang.feature.mine.contract.CertificationContract;
import com.yiqi.liebang.feature.mine.contract.WorkContract;
import com.yiqi.liebang.feature.mine.dependencies.certification.CertificationPresenterModule;
import com.yiqi.liebang.feature.mine.dependencies.certification.DaggerCertificationComponent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.yiqi.liebang.common.util.DateTool.DEFAULT_SDF;

public class UploadEduActivity extends BaseActivity implements OnOperItemClickL, ActionSheetDialog.MyClickListenerInterface, Toolbar.OnMenuItemClickListener, CertificationContract.View {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.view_all)
    LinearLayout mViewAll;
    @BindView(R.id.btn_upload_edu_xwz)
    ImageView mBtnUploadEduXwz;
    @BindView(R.id.btn_upload_edu_byz)
    ImageView mBtnUploadEduByz;
    @BindView(R.id.view_tip_xwz)
    LinearLayout mViewTipXwz;
    @BindView(R.id.view_tip_byz)
    LinearLayout mViewTipByz;

    private ActionSheetDialog dialog;
    private List<LocalMedia> selectList = new ArrayList<>();
    private String mXWZImageUrl;
    private String mBYZImageUrl;
    private EducationBo mImages;
    private boolean isAll = false;
    @Inject
    CertificationContract.Presenter mPresenter;


    private int mSelectType = 1;

    private String mEduId = "";
    private String mUploadImg = "";
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_upload_education;
    }


    @Override
    protected void initInjector() {
        DaggerCertificationComponent.builder().certificationPresenterModule(new CertificationPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "上传证明材料", true, true);
        mToolbar.setOnMenuItemClickListener(this);
        final String[] stringItems = {"拍照", "从手机相册选择"};
        dialog = new ActionSheetDialog(this, stringItems, null);
        dialog.isTitleShow(false);
        dialog.setOnOperItemClickL(this);
        dialog.setListener(this);
    }

    @Override
    protected void initData() {
        mEduId = this.getIntent().getStringExtra("edu_id");
        mImages = (EducationBo) this.getIntent().getSerializableExtra("edu_imgs");
        isAll = this.getIntent().getBooleanExtra("isAll", false);
        if (mImages != null) {
                if (TextUtils.isEmpty(mImages.getDiplomaImage())) {
                    AM.image().bind(mImages.getDiplomaImage(), mBtnUploadEduByz);

                } else if (TextUtils.isEmpty(mImages.getDegreeImage())) {
                    AM.image().bind(mImages.getDiplomaImage(), mBtnUploadEduXwz);
                }
            }

        mToolbar.inflateMenu(isAll ? R.menu.menu_save : R.menu.menu_commit);
    }

    @Override
    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
        mViewTipXwz.setVisibility(View.GONE);
        mViewTipByz.setVisibility(View.GONE);
        switch (position) {
            case 0:
                PictureSelector.create(this)
                        .openCamera(PictureMimeType.ofImage())
                        .theme(R.style.picture_default_style1)
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                        .previewImage(true)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .isCamera(true)
                        .enableCrop(true)// 是否裁剪
                        .circleDimmedLayer(false)// 是否圆形裁剪 true or false
//                        .withAspectRatio(1, 1)// int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                        .compress(true)// 是否压缩
                        .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                        .isGif(false)// 是否显示gif图片
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                        .circleDimmedLayer(false)// 是否圆形裁剪
                        .selectionMedia(selectList)// 是否传入已选图片
                        .scaleEnabled(true)
                        .forResult(000);//结果回调onActivityResult code
                break;
            case 1:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .theme(R.style.picture_default_style1)
                        .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                        .previewImage(true)// 是否可预览图片
                        .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                        .isCamera(true)
                        .enableCrop(true)// 是否裁剪
                        .circleDimmedLayer(false)// 是否圆形裁剪 true or false
                        .compress(true)// 是否压缩
                        .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                        .isGif(false)// 是否显示gif图片
                        .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                        .circleDimmedLayer(false)// 是否圆形裁剪
                        .selectionMedia(selectList)// 是否传入已选图片
                        .scaleEnabled(true)
                        .forResult(999);//结果回调onActivityResult code
                break;

        }

        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void doCancle() {
        mViewTipXwz.setVisibility(View.GONE);
        mViewTipByz.setVisibility(View.GONE);

        dialog.dismiss();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 000:
            case 999:
                // 图片选择结果回调
                selectList = PictureSelector.obtainMultipleResult(data);

                switch (mSelectType) {
                    case 1:
                        mBYZImageUrl=null;
                        for (int i = 0; i < selectList.size(); i++) {
                            mXWZImageUrl = selectList.get(i).getCutPath();

                        }
                        break;
                    case 2:
                        mXWZImageUrl=null;
                        for (int i = 0; i < selectList.size(); i++) {
                            mBYZImageUrl = selectList.get(i).getCutPath();

                        }
                        break;
                }


                if (!TextUtils.isEmpty(mXWZImageUrl)){
                    mUploadImg=mXWZImageUrl;
                    AM.image().bind(mXWZImageUrl, mBtnUploadEduXwz);
                }else {
                    AM.image().bind(R.drawable.img_xueweizheng, mBtnUploadEduXwz);
                }

                if (!TextUtils.isEmpty(mBYZImageUrl)){
                    mUploadImg=mBYZImageUrl;
                    AM.image().bind(mBYZImageUrl, mBtnUploadEduByz);
                }else {
                    AM.image().bind(R.drawable.img_biyezheng, mBtnUploadEduByz);
                }


        break;
    }

}


    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (isAll) {
            mPresenter.uploadAuthenticationImage(mEduId,mSelectType+"",mUploadImg);
        } else {
            mPresenter.authEdu(mEduId, mBYZImageUrl, mXWZImageUrl);
        }
        return false;
    }

    @OnClick({R.id.btn_upload_edu_xwz, R.id.btn_upload_edu_byz})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_upload_edu_xwz:
                mSelectType = 1;
                mViewTipXwz.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_upload_edu_byz:
                mSelectType = 2;
                mViewTipByz.setVisibility(View.VISIBLE);
                break;
        }

        dialog.show();
    }


    @Override
    public void showWorkSuccess(WorkBo workBo) {

    }

    @Override
    public void showUserCardSuccess(UserCardBo s) {

    }

    @Override
    public void showEducationSuccess(EducationBo workBo) {

    }

    @Override
    public void showAuthWorkSuccess() {
        if (isAll) {
            finish();
        } else {
            Intent intents = new Intent(this, CommitSuccessActivity.class);
            intents.putExtra("date", DateTool.getCurTimeString(DEFAULT_SDF));
            startActivity(intents);
            finish();
        }


    }

    @Override
    public void showCertificationSuccess() {

    }

    @Override
    public void showError(String error) {
        showErrorMsg(error);
    }

    @Override
    public void showWorkListSuccess(List<WorkBo> workBos) {

    }

    @Override
    public void showEduListSuccess(List<EducationBo> workBos) {

    }

    @Override
    public void showUploadAuthenticationImageSuccess() {
      showMsg("保存成功！");
      finish();
    }
}
